<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("base/BaseImageForm.php");
require_once("cms/core/om/Image.php");

class ImageForm extends BaseImageForm {

    private $log;

    public function __construct() {
        parent::__construct();
        $this->log =& LoggerManager::getLogger("ImageForm");
        $this->getTinyImage()->setImageRelatedByImageId(new Image());
        $this->getTinyImage()->setImageRelatedByThumbnailId(new Image());
    }

    public function reset(ActionConfig $mapping, Request $request) {
        $this->base = $request->getContext()->getRealPath("");
    }

    public function validate() {

        global $ALLOWABLE_IMAGES;

        $ALLOWABLE_IMAGES = array(
            "image/jpeg"=>"jpg",
            "image/pjpeg"=>"jpg",
            "image/gif"=>"gif",
            "image/png"=>"png"
        );

        $errors = parent::validate();

        if($this->getTinyImage()->getTinyImageId() == "" &&
            $this->getPath() == "" &&
            (is_null($this->formImage) || !is_array($this->formImage) || $this->formImage["size"] == 0))
                $errors->add("formImage",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.image", true)));

        if(strlen($this->getPath()) > 0) {
            $path = $this->base . (strpos($this->getPath(), "/") === 0 ? $this->getPath() : "/".$this->getPath());
            if(!is_dir($path))
                $errors->add("formImage",
                    new ActionMessage("error.path", true, new ActionMessage($path, false)));
        }

        return $errors;
    }

}
?>